﻿package controllers.read;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rbac.models.Oplog;
import rbac.models.Privilege;
import rbac.models.PrivilegeType;
import rbac.models.User;

import models.Esexcerpt;

import com.et.mvc.BinaryView;
import com.et.mvc.JsonView;
import com.et.mvc.MultipartFile;
import com.et.mvc.TextView;
import com.et.mvc.View;

import com.et.mvc.filter.BeforeFilter;
import com.et.mvc.filter.BeforeFilters;

import controllers.ApplicationController;


public class EsexcerptController extends ApplicationController {
	
	public void index() throws Exception{
    	
	}
	
	public View getEsexcerptItems(int page, int rows, String q,int t, String sort, String order) throws Exception {
		if (page == 0) page = 1;
		if (rows == 0) rows = 50;
		String orderBy = "sort";
		if (sort != null && order != null){
			orderBy = sort + " " + order;
		}
		String cond = "1=1";
		List<Object> tmpArgs = new ArrayList<Object>();
		
		
		
		/**
		if (q != null){
			cond += " and (name like '%"+q+"%' or memo like '%"+q+"%' ) ";			
		}
		**/
		User user = (User) rbac.getCurrentUser();
		cond += " and uid = '" + user.getLogin() + "' ";
		Object[] args = tmpArgs.toArray();
		
		long total = Esexcerpt.count(Esexcerpt.class, cond, args);
		List<Esexcerpt> items = Esexcerpt.findAll(Esexcerpt.class, cond, args, orderBy, rows, (page-1)*rows);
		
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("total", total);
		result.put("rows", items);
		
		return new JsonView(result);
	}
	
	public View saveEsexcerpt(Esexcerpt item, String[] rids) throws Exception {
		Map<String,Object> result = new HashMap<String,Object>();
		//其他需要系统自动填写的字段
		//其他操作如写入日志
		Integer sort = (Integer)Esexcerpt.maximum(Esexcerpt.class, "sort", null, null);
		if (sort == null){
			sort = 1;
		} else {
			sort += 1;
		}
		item.setSort(sort);
		item.setInputdate(new java.sql.Timestamp(System
				.currentTimeMillis()));
		item.setEditdate(new java.sql.Timestamp(System
				.currentTimeMillis()));
		User user = (User) rbac.getCurrentUser();
		item.setUid(user.getLogin());
		if (item.save()){
			Oplog.addMsg(user.getLogin(), user.getName() + "("+getIpAddr(request)+")新建房源摘录，内容为：" + new JsonView(item));									
			
			result.put("success", true);
		} else {
			result.put("failure", true);
			result.put("msg", getErrorMsg(item));
		}
		
		View view = new JsonView(result);
		view.setContentType("text/plain;charset=utf-8");
		return view;
	}
	
	public View updateEsexcerpt(int id, Map<String,Object> params, String[] rids) throws Exception {
		Map<String,Object> result = new HashMap<String,Object>();
		Esexcerpt item = Esexcerpt.find(Esexcerpt.class, id);
		JsonView olditem= new JsonView(item);
		item = Esexcerpt.updateModel(item, params);
    //其他需要系统自动填写的字段
    //其他操作如写入日志		
		item.setEditdate(new java.sql.Timestamp(System
				.currentTimeMillis()));
		User user = (User) rbac.getCurrentUser();
		item.setUid(user.getLogin());
		if (item.save()){
			Oplog.addMsg(user.getLogin(), user.getName() + "("+getIpAddr(request)+")修改房源摘录，原内容为："+ olditem + "\r\n修改内容为：" + new JsonView(item));										
			
			result.put("success", true);
		} else {
			result.put("failure", true);
			result.put("msg", getErrorMsg(item));
		}
		
		View view = new JsonView(result);
		view.setContentType("text/plain;charset=utf-8");
		return view;
	}
	
	public View destroyEsexcerpt(int id) throws Exception {
		Esexcerpt item = Esexcerpt.find(Esexcerpt.class, id);
		User user = (User) rbac.getCurrentUser();
		if ((isAdmin()) || (item.getUid().equals(user.getLogin())))
		{
			Oplog.addMsg(user.getLogin(), user.getName() + "("+getIpAddr(request)+")删除房源摘录，内容为：" + new JsonView(item));									
			
			item.destroy();
		}
		
		return new JsonView("success:true");
	}
	
	public View addRow(int id, int extid) throws Exception {
		Esexcerpt item = Esexcerpt.find(Esexcerpt.class, extid);
		JsonView olditem= new JsonView(item);
		if (item != null) {
			String idS = item.getContent();
			if (idS == null) idS = "";
			
			String idS1 = "," + idS + ",";
			if (idS1.indexOf(","+String.valueOf(id)+",") == -1){
			  if (!idS.equals("")) idS += ",";
			  idS += String.valueOf(id);
			  item.setContent(idS);
			  item.save();
			  User user = (User) rbac.getCurrentUser();
			  Oplog.addMsg(user.getLogin(), user.getName() + "("+getIpAddr(request)+")修改房源摘录，原内容为："+ olditem + "\r\n修改内容为：" + new JsonView(item));										
				
			}
		}
		return new JsonView("success:true");
	}
	
	public View removeRow(int id, int extid) throws Exception {
        Esexcerpt item = Esexcerpt.find(Esexcerpt.class, extid);
        JsonView olditem= new JsonView(item);
		if (item != null) {
			String idS = item.getContent();
			if (idS == null) idS = "";
			idS = "," + idS + ",";
			idS = idS.replace(","+String.valueOf(id)+",", ","); 
			if (idS.startsWith(","))   
				idS=idS.substring(",".length()); 
			if(idS.endsWith(","))   
				idS=idS.substring(0,idS.length()-",".length()); 
			item.setContent(idS);
			item.save();
			User user = (User) rbac.getCurrentUser();
			Oplog.addMsg(user.getLogin(), user.getName() + "("+getIpAddr(request)+")修改房源摘录，原内容为："+ olditem + "\r\n修改内容为：" + new JsonView(item));										
				
		}
		return new JsonView("success:true");
	}
	
	
	public View move(int id, String dir) throws Exception {
		Esexcerpt item = Esexcerpt.find(Esexcerpt.class, id);
		Esexcerpt target = null;
		if (dir.equals("down")) {
			target = Esexcerpt.findFirst(Esexcerpt.class, "sort>?",
					new Object[] { item.getSort() }, "sort asc");
		} else {
			target = Esexcerpt.findFirst(Esexcerpt.class, "sort<?",
					new Object[] { item.getSort() }, "sort desc");
		}
		if (target != null) {
			Integer tmp = target.getSort();
			target.setSort(item.getSort());
			target.save();
			item.setSort(tmp);
			item.save();
		}
		return new JsonView("success:true");
	}
}
